package com.example.redissondemo.utils;

import io.micrometer.common.util.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * @author Liunh
 */
public class NumberUtil {

    private static volatile NumberUtil instance = null;

    private NumberUtil() {
    }

    public static NumberUtil getInstance() {
        if (null == instance) {
            synchronized (NumberUtil.class) {
                if (null == instance) {
                    instance = new NumberUtil();
                }
            }
        }
        return instance;
    }

    public double setDoubleScale(double oldValue, int newScale) {
        final BigDecimal bigDecimal = new BigDecimal(oldValue).setScale(newScale, RoundingMode.HALF_UP);
        return bigDecimal.doubleValue();
    }

    public boolean isNumber(String data) {
        if (StringUtils.isNotBlank(data) && data.matches(Constant.REGEXP_NUMBER)) {
            return true;
        }
        return false;
    }

    public BigDecimal setBigDecimalScale(double oldValue, int newScale) {
        return new BigDecimal(oldValue).setScale(newScale, RoundingMode.HALF_UP);
    }

    public BigDecimal setBigDecimalScale(BigDecimal oldValue, int newScale) {
        return oldValue.setScale(newScale, RoundingMode.HALF_UP);
    }

}
